package su.mvc.action.admin;

import com.googlecode.objectify.Key;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import su.mvc.action.ActionSupport;
import su.mvc.model.Image;
import su.mvc.service.UserService;
import su.mvc.service.gui.ImageService;
import su.mvc.service.gui.MenuService;

import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

@Results(value = {
        @Result(name=ActionSupport.SUCCESS, location="images.jsp"),
        @Result(name = ActionSupport.REDIRECT_CANCEL, type = "redirect", location = "/admin/images"),
        @Result(name = ActionSupport.REDIRECT_OK, type = "redirect", location = "/admin/images", params = {"messages", "image.deleted.successfully"}),
        @Result(name = ActionSupport.ACCESS_DENIED, location = "access-denied.jsp")
})
public class ImagesAction extends ActionSupport {

    private static final Logger log = Logger.getLogger(ImagesAction.class.getName());

    private ImageService imageService;

    private MenuService menuService;

    private UserService userService;

    private Key parentItemKey;

    private Map<String, String> menuItems;

    private List<Image> images;
    
    private Long imageId;
    
    public String execute() throws Exception{
        if(!userService.isUserAdmin()){
            return ACCESS_DENIED;
        }
        menuItems = menuService.getMenuItemsMap("-");
        images = imageService.getImagesByMenuItem(parentItemKey);
        return SUCCESS;
    }
    
    public String delete() throws Exception {
        if(!userService.isUserAdmin()){
            return ACCESS_DENIED;
        }
        if (imageId != null) {
            imageService.deleteImage(imageId);
        }
        return REDIRECT_OK;
    }

    public UserService getUserService() {
        return userService;
    }

    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    public ImageService getImageService() {
        return imageService;
    }

    public void setImageService(ImageService imageService) {
        this.imageService = imageService;
    }

    public MenuService getMenuService() {
        return menuService;
    }

    public void setMenuService(MenuService menuService) {
        this.menuService = menuService;
    }

    public Key getParentItemKey() {
        return parentItemKey;
    }

    public void setParentItemKey(Key parentItemKey) {
        this.parentItemKey = parentItemKey;
    }

    public Map<String, String> getMenuItems() {
        return menuItems;
    }

    public void setMenuItems(Map<String, String> menuItems) {
        this.menuItems = menuItems;
    }

    public List<Image> getImages() {
        return images;
    }

    public void setImages(List<Image> images) {
        this.images = images;
    }

    public Long getImageId() {
        return imageId;
    }

    public void setImageId(Long imageId) {
        this.imageId = imageId;
    }
}
